package fun.shuofeng.juc.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 朔风
 * @date 2024-02-01 18:08
 */
public class ThreadPoolDemo {
    public static void main(String[] args) {
        // 一池一线程
        ExecutorService tp1 = Executors.newSingleThreadExecutor();
        // 一池N线程
        ExecutorService tp2 = Executors.newFixedThreadPool(8);
        // 可扩容线程
        ExecutorService tp3 = Executors.newCachedThreadPool();


        try {
            for (int i = 0; i < 10; i++) {
                tp1.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " 被执行了");
                });
            }

            for (int i = 0; i < 10; i++) {
                tp2.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " 被执行了");
                });
            }

            for (int i = 0; i < 100; i++) {
                tp3.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + " 被执行了");
                });
            }
        } finally {
            tp1.shutdown();
            tp2.shutdown();
            tp3.shutdown();
        }


    }
}

